﻿var win = null; //单窗口
var login = null;
var regwin = null;
var regformpanel = null;



function reguser() {
    if (regwin != null) {
        if (regwin.hidden) {//当窗口隐藏时
            regwin.show();
        }
    } else {
        regwin = new Ext.Window({
            layout: "fit",
            title: "注册新用户",
            width: 300,
            height: 150,
            collapsible: true, //折叠按钮
            closable: true, //关闭按钮
            closeAction: "hide",
            border: false,
            items: [regformpanel]
        });
        regwin.show();
    }
}

Ext.onReady(function () {
    Ext.BLANK_IMAGE_URL = "images/s.gif";
    Ext.QuickTips.init();


    login = new Ext.FormPanel({
        labelWidth: 60,
        frame: true,

        defaultType: "textfield",
        monitorValid: true, //提交按钮就与表单的验证绑在一起了,只有全部验证通过时,表单提交按钮才是enable的,否则,该按钮不可以点击

        items: [{
            id: "idloginUsername",
            fieldLabel: "用户名",
            name: "loginUsername",
            emptyText: '请输入用户名',
            allowBlank: false,
            enableKeyEvents: true,
//            listeners: {
//                keyup: function () {
//                    if (Ext.EventObject.getKey() == Ext.EventObject.ENTER) {
//                        //loginwup();
//                        // selectOption()
//                        window.location = 'selectOptions.aspx';
//                    }
//                }
//            }

        }, {
            id: "idloginPassword",
            fieldLabel: "密  码",
            name: "loginPassword",
            emptyText: '请输入密码',
            inputType: "password",
            allowBlank: false,
            enableKeyEvents: true,
//            listeners: {
//                keyup: function () {
//                    if (Ext.EventObject.getKey() == Ext.EventObject.ENTER) {
//                        //loginwup();
//                        //selectOption()
//                        window.location = 'selectOptions.aspx';
//                    }
//                }
//            }
        }],

        // All the magic happens after the user clicks the button 
        buttons: [{
            text: "登陆",
            formBind: true,
            // Function that fires when user clicks the button 
            handler: function () {
                login.getForm().submit({
                    method: "POST",
                    url: "./Services/login.aspx",
                    waitTitle: "连接中...",
                    waitMsg: "正在发送数据...",
                    success: function (form, action) {
                        win.hide();

                        window.location = 'selectOptions.aspx';

//                        myWindow = window.open('selectOptions.aspx');
//                        myWindow.focus();
                    },
                    failure: function (form, action) {
                        if (action.failureType == "server") {
                            obj = Ext.decode(action.response.responseText);
                            Ext.Msg.alert("登录失败!", obj.reason);
                            //Ext.Msg.alert("登录失败1", "请稍候再尝试！");
                        } else {
                            //Ext.Msg.alert("请稍等!", "服务器无法访问?: " + action.response.responseText);
                            Ext.Msg.alert("登录失败", "与服务器通讯中断，请检查网络，稍候再尝试！");
                        }
                        login.getForm().reset();
                    }
                });
            }
        }],
        //按住shift+Enter事件
        keys: [{
            key: [10, 13],
            shift: true//,
            //fn: alert("a")
        }]
    });


    regformpanel = new Ext.FormPanel({
        labelWidth: 60,
        frame: true,
        //title:"登录系统", 
        defaultType: "textfield",
        monitorValid: true, //提交按钮就与表单的验证绑在一起了,只有全部验证通过时,表单提交按钮才是enable的,否则,该按钮不可以点击
        items: [{
            fieldLabel: "用户名",
            name: "loginUsername",
            allowBlank: false
        }, {
            fieldLabel: "密  码",
            name: "loginPassword",
            inputType: "password",
            allowBlank: false
        }],

        // All the magic happens after the user clicks the button 
        buttons: [{
            text: "注册",
            formBind: true,
            // Function that fires when user clicks the button 
            handler: function () {
                regformpanel.getForm().submit({
                    method: "POST",
                    url: "./Services/reguser.aspx",
                    waitTitle: "连接中...",
                    waitMsg: "正在发送数据...",

                    success: function (form, action) {
                        regwin.hide();
                        Ext.Msg.alert("注册成功", "请记住您的用户名和密码!");
                    },
                    failure: function (form, action) {
                        if (action.failureType == "server") {
                            obj = Ext.util.JSON.decode(action.response.responseText);
                            //Ext.Msg.alert("注册失败!", obj.errors.reason); 
                            Ext.Msg.alert("注册失败", "请稍候再尝试！");
                        } else {
                            //Ext.Msg.alert("请稍等!", "服务器无法访问?: " + action.response.responseText); 
                            Ext.Msg.alert("注册失败", "请稍候再尝试！");
                        }
                        regformpanel.getForm().reset();
                    }
                });
            }
        }]
    });
    Ext.get("loginbutton").on("click", openloginWin);
});

function openloginWin() {
    if (win != null) {
        if (win.hidden) {//当窗口隐藏时
            win.show();
        }
    } else {
        win = new Ext.Window({
            layout: "fit",
            title: "登录系统",
            width: 300,
            height: 150,
            collapsible: true, //折叠按钮
            closable: true, //关闭按钮
            closeAction: "hide",
            border: false,
            items: [login]
        });
        win.show();
    }
}